/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { PathUtil } from '@ohos/hvigor';
import * as fs from 'fs';
import * as path from 'path';

import { initByStageEmptyApp, runHvigor, } from './common/common';

describe('test trace Stage app', () => {
  test.concurrent('stage app entry trace', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      await traceTest(projectDir, 'entry');
    });
  });
});

export function traceTest(projectDir: string, moduleName: string) {
  const commands = ['-m', 'module',
    '-p', `module=${moduleName}`
  ];
  commands.push('assembleHap');
  commands.push('--no-daemon');
  const result = runHvigor(projectDir, commands);
  expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

  const detailsJsonPath = path.resolve(PathUtil.getHvigorCacheDir(), 'outputs', 'logs', 'details', 'details.json');
  const detailsJson = JSON.parse(fs.readFileSync(detailsJsonPath).toString());
  expect(detailsJson.totalTime > 0).toBeTruthy();
  expect(detailsJson.moduleNum).toEqual(1);
  expect(detailsJson.taskTime).toBeDefined();
  expect(detailsJson.taskTime.compileArkTS > 0 || detailsJson.taskTime.buildArkTS > 0).toBeTruthy();
  expect(detailsJson.taskTime.compileJS > 0 || detailsJson.taskTime.buildJS > 0).toBeTruthy();
  expect(detailsJson.taskTime.compileResource > 0).toBeTruthy();
  expect(detailsJson.taskTime.packageHap > 0).toBeTruthy();
  expect(detailsJson.taskTime.signHap > 0).toBeTruthy();
  expect(detailsJson.isIncremental).toEqual(true);
  expect(detailsJson.hasIncremental).toEqual(false);
  expect(detailsJson.isParallel).toEqual(true);
}
